<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <jsp:include page="/resources/inc/head.jsp" />
    <link href="${basePath}/resources/zheng-admin/plugins/zTree_v3/css/demo.css" rel="stylesheet"/>
</head>
<body>
<form class="layui-form layui-form-pane" id="_form">
    <div class="layui-form-item">
        <label class="layui-form-label">商品分类名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入商品分类名称"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">引用父属性</label>
        <div class="layui-input-block">
            <input type="radio" name="copyAttribute" value="false" title="不引用" checked="">
            <input type="radio" name="copyAttribute" value="true" title="引用">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">引用父参数</label>
        <div class="layui-input-block">
            <input type="radio" name="copyParam" value="false" title="不引用" checked="">
            <input type="radio" name="copyParam" value="true" title="引用">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请选择父分类</label>
        <div class="layui-input-block">
            <input type="text" readonly="readonly" lay-verify="required" id="pid"
                   autocomplete="off" placeholder="请选择父分类" class="layui-input" onclick="showMenu()">
            <input type="hidden" name="pid" id="pid_value">
        </div>
        <div id="menuContent" class="menuContent" style="display:none; position: absolute;">
            <ul id="ztree" class="ztree" style="margin-top:0; width:220px;"></ul>
        </div>
    </div>
    <div class="layui-form-item" style="margin-top: 50px;">
        <div class="layui-input-block" style="float: right">
            <button class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
        </div>
    </div>
</form>
<jsp:include page="/resources/inc/footer.jsp" />
<script>
    $(function () {
        var url = "${basePath}/manage/product/category/create";
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        commonSubmit(url, "sub", index);
    });
    var getFormData = function (data) {
        return JSON.stringify(data.field);
    }
    var setting = {
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };
    var zNodes = [{id: 0, name: "顶级", open: true}];
    <c:forEach var="productCategory" items="${productCategoryList}">
    zNodes.push({id:${productCategory.id}, pId:${productCategory.pid}, name: "${productCategory.name}"});
    </c:forEach>
    function onClick(e, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("ztree"),
                nodes = zTree.getSelectedNodes(),
                v = "";
        nodes.sort(function compare(a, b) {
            return a.id - b.id;
        });
        for (var i = 0, l = nodes.length; i < l; i++) {
            v += nodes[i].name + ",";
        }
        if (v.length > 0) v = v.substring(0, v.length - 1);
        var pid = $("#pid");
        var pid_value = $("#pid_value");
        pid_value.val(treeNode.id);
        pid.val(v);
    }
    function showMenu() {
        var pid = $("#pid");
        var pidOffset = $("#pid").offset();
        $("#menuContent").css({
            left: pidOffset.left + "px",
            top: pidOffset.top + pid.outerHeight() - 12 + "px"
        }).slideDown("fast");
        $("body").bind("mousedown", onBodyDown);
    }
    function hideMenu() {
        $("#menuContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }
    function onBodyDown(event) {
        if (!(event.target.id == "pid" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
            hideMenu();
        }
    }
    $(document).ready(function () {
        $.fn.zTree.init($("#ztree"), setting, zNodes);
    });
    var p = "${productCategory}";
    if (p) {
        $("#pid").val("${productCategory.name}");
        $("#pid_value").val("${productCategory.id}");
    }
</script>
</body>
</html>
